package com.macro.mall.task;

import cn.hutool.core.util.StrUtil;
import com.macro.mall.model.RcRegistrationContent;
import com.macro.mall.service.RcRegistrationContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;

/**
 * @author super
 * @title: TaskProcessor
 * @description: TODO 订单关闭处理器
 * @date 2020/9/22 11:02
 */


@Slf4j
@Configuration
@EnableScheduling
public class RCStatusTaskProcessor {

    private static final String KEY = "ORDER_CLOSE";
    @Autowired
    private StringRedisTemplate redisTemplate;
    /** RcRegistrationContentService服务 */
    @Autowired
    private RcRegistrationContentService rcRegistrationContentService;
    @Scheduled(cron = "0 */5 * * * ?")
    public void orderClose() {
        log.info(new Date() + "  >>cron执行-修改活动状态定时任务启动 Timer begin...");
        try {
            ValueOperations objOps = redisTemplate.opsForValue();
            boolean bSet = objOps.setIfAbsent(KEY, "processing");
            if (!bSet) {
                String message = "processor is running, cannot start again.";
                log.error(message + " the key is redis is :" + KEY);
                return;
            }
            RcRegistrationContent build = RcRegistrationContent.builder().status("4").build();
            List<RcRegistrationContent> rcRegistrationContents = rcRegistrationContentService.find(build,1,9999,"task");
            for(RcRegistrationContent one : rcRegistrationContents) {
                String flag = getFlag(one);
                if(flag != null) {
                    rcRegistrationContentService.updateNotNullById(RcRegistrationContent.builder().id(one.getId()).flag(flag).build());
                }
            }
        } catch (Exception e) {
            log.error("orderClose ~~~~~~~~~~~~~~~~~~~~~~~~~~", e);
        } finally {
            log.info(new Date() + "  >>cron执行-修改活动状态定时任务启动 Timer end.");
            Boolean delete = redisTemplate.delete(KEY);
        }

    }

   public String getFlag(RcRegistrationContent one) {
        long l = System.currentTimeMillis();
        //志愿活动状态 1-活动未开始 2-活动结束 3-报名未开始 4-活动报名中 5-已报名 6-名额已满
        String flag = one.getFlag();
        if (StrUtil.isBlank(flag) || !flag.equals("5") || !flag.equals("6")) {
            if (one.getSRegistration() != null && one.getSRegistration().getTime() > l) {
                return "3";
            } else if ((one.getSRegistration() != null && one.getERegistration() != null) && one.getSRegistration().getTime() < l && one.getERegistration().getTime() > l) {
                return "4";
            }else if (one.getStime().getTime() > l) {
                return "1";
            } else if (one.getEtime().getTime() < l) {
                return "2";
            }
        }
        return null;
    }


}
